<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>概念管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/css/global.css" media="all">
    <link rel="stylesheet" href="/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/table.css"/>
</head>

<body>
<div class="admin-main">
    <blockquote class="layui-elem-quote">
        <div class="demoTable">
         <div class="layui-inline" style="margin-bottom: 10px;">
            搜索名称：
            <div class="layui-inline">
                <input id="SEARCH_ID" class="layui-input" name="id" autocomplete="off" placeholder="请输入概念名称"/>
            </div>
            </div>
             <div class="layui-inline" style="margin-bottom: 10px;">
            <button class="layui-btn" data-type="reload" id="search" >搜索</button>
            <button class="layui-btn layui-btn-primary" onclick="resetSearch()">重置</button>
            <a data-opt="add" href="/manage/conceptCategory/addConceptCategory" class="layui-btn"><i class="layui-icon">&#xe608;</i>添加概念</a>
        </div>
        </div>
    </blockquote>
    
            <span style="font-size: 16px;"><b>概念列表</b></span>
                <table class="layui-hide" id="tableID" lay-filter="tableFilter"></table>
</div>


<template id="editTemplate" hidden>
    <div style="margin: 15px;">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>编辑概念(<label class="layui-required-tip">标红的为必填项</label>)</legend>
        </fieldset>

        <form id="check-edit-form" class="layui-form js-form" action="/manage/conceptCategory/updateConceptCategory" method="post">
            <input type="hidden" name="id" class="layui-input js-id" />
            <div class="layui-form-item">
                <label class="layui-form-label layui-required-tip">概念名称：</label>
                <div class="layui-input-block">
                    <input type="text" id="FORM_TITLE" name="conceptCategoryName" maxlength="50" lay-verify="required" placeholder="请填写概念名称"  autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label layui-required-tip">简介</label>
                <div class="layui-input-block">
                    <textarea id="CONTENT" name="content" placeholder="简介" class="layui-textarea" lay-verify="required"></textarea>
                </div>
            </div>
            <div class="layui-form-item"style="display: none;">
                <label class="layui-form-label">状态：</label>
                <div class="layui-input-block">
                    <input type="radio" class="js-status1" name="enableFlag" value="1"  title="启用" />
                    <input type="radio" class="js-status0" name="enableFlag" value="0" title="停用" />
                </div>
            </div>
            <button lay-filter="edit" lay-submit style="display: none;"></button>
        </form>
    </div>
    <script>
        layui.use(['form', 'layedit', 'laydate'], function() {
            var form = layui.form;
            form.render();
        });
    </script>
</template>

<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<script type="text/javascript" src="/js/global-config.js"></script>
<script>
    layui.config({
        base: '/js/'
    });

    function resetSearch() {
        SEARCH_ID.value = '';
    }
    

    layui.use(['form', 'laydate','table','laydate','util'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,//表格
            laydate = layui.laydate,//日期
            util = layui.util;//工具类

        
        
      pageInit();
      //执行一个 table 实例
      function pageInit(){
    	   table.render({
               elem: '#tableID'
               ,url:'/manage/conceptCategory/listConceptCategory'
               ,method: 'post'
               ,cols: [[
                   {field:'id', title: 'ID', sort: true}
                   ,{field:'conceptCategoryName', title: '概念名称'}
                   ,{field:'content', title: '概念简介'}
                   ,{field:'enableFlag', title: '状态' , templet:function(d){
                	   if(d.enableFlag==false){
                		  return '<div class="layui-btn layui-btn-danger layui-btn-xs">已停用 </div>'
  		 	    	  }else{
  		 	    		 return '<div class="layui-btn layui-btn-xs">使用中</div>'
  		 	    	  }
                    }}
                   ,{field:'updateTime', title: '修改时间', sort: true,templet: function(d){
                	   if(null!=d.updateTime){
                		   return util.toDateString(d.updateTime, 'yyyy-MM-dd HH:mm:ss');
                	   }else{
                		   return '';
                	   }
                       
                   }}
                   ,{title: '操作', align:'left', templet:function(d){
                      var tool = ' <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>';
                	  if(d.enableFlag==true){
                           tool+='<a class="layui-btn layui-btn-xs  layui-btn-warm" lay-event="del">停用 </a>';
 		 	    	  }else{
                           tool+='<a class="layui-btn layui-btn-xs" lay-event="del">启用</a>';
 		 	    	  }
                       return tool;
                   }}
               ]]
               ,page: true
           });
      } 

       
      //监听工具条
        table.on('tool(tableFilter)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            var editBoxIndex = -1;
            if(layEvent === 'del'){
            	var id = data.id;
            	var enableFlag = data.enableFlag;
                //配置一个透明的询问框
                layer.confirm('确定要设置吗?', {
                    btn : [ '确定', '取消' ]//按钮
                }, function(index) {
                    $.ajax({
                        cache: true,
                        type: "GET",
                        url: "/manage/conceptCategory/updateConceptCategoryStatus",
                        data: "id="+id+"&enableFlag="+enableFlag,
                        async: false,
                        error: function () {
                            layer.close(index);
                            alert("Connection error");
                        },
                        success: function (data) {
                            layer.close(index);
                            table.reload("tableID");
                        }
                    });
                });
            } else if(layEvent === 'edit'){
            	if (editBoxIndex !== -1) {
                    return;
                }
                var template = $('#editTemplate').html();
                var ctx = $(this);
                editBoxIndex = layer.open({
                    type: 1,
                    title: '编辑表单',
                    content: template,
                    btn: ['保存', '取消'],
                    area: ['800px', '60%'],
                    yes: function (index) {
                        //触发表单的提交事件
                        $('#layui-layer' + editBoxIndex).find('.js-form [lay-submit]').click();
                    },
                    full: function (elem) {
                        var win = window.top === window.self ? window : parent.window;
                        $(win).on('resize', function () {
                            var $this = $(this);
                            elem.width($this.width()).height($this.height()).css({
                                top: 0,
                                left: 0
                            });
                            elem.children('div.layui-layer-content').height($this.height() - 95);
                        });
                    },
                    success: function (layero, index) {
                        //弹出窗口成功后渲染表单
                        form.render();
                    },
                    end: function () {
                    	editBoxIndex = -1;
                    }
                });

                layer.ready(function () {
                    var $layer = $('#layui-layer' + editBoxIndex);
                    //ID
                    $layer.find('.js-id').val(data.id);
                    //名称
                    FORM_TITLE.value = data.conceptCategoryName;
                    //状态
                    var status = data.enableFlag==true?1:0;

                    CONTENT.value = data.content;

                    $layer.find('.js-status' + status).attr('checked', 'checked');
                    form.render();
                    
                    form.on('submit(edit)', function (data) {
                        //这里可以写ajax方法提交表单
                        $.ajax({
                            cache: false,
                            type: "POST",
                            url: "/manage/conceptCategory/updateConceptCategory",
                            data: $("#check-edit-form").serialize(),
                            async: true,
                            error: function () {
                                alert("Connection error");
                            },
                            success: function (msg) {
                                pageInit();
                                layer.closeAll();
                            }
                        });
                        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                    });

                })
            }
        });
      
        var $ = layui.$, active = {
                reload: function(){
                    //执行重载
                    table.reload('tableID', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            find : SEARCH_ID.value
                        }
                    });
                }
            };

            $('.demoTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

        $(".layui-input").keydown(function (e) {
            if(e.keyCode == 13){
                $('#search').click();
            }
        });

    });
</script>
</body>

</html>